
        
local BtnHLWS = 0x01
local BtnSJSK = 0x02
local BtnZJGW = 0x03
local BtnWXHL = 0x04

local BtnZY     = 0x05
local BtnSD     = 0x06
local BtnCJ     = 0x07

local BtnInfo   = 0x09
local BtnMusic  = 0x0a
local BtnSound  = 0x0b
local BtnWeibo  = 0x0c

local Layer_Color = 0x11

-- 菜单显示层
local layer = nil
local scene = nil

-- 全局计时器
local _frame = 0
local _lastResTime = os.clock()
local function update(dt)
    _frame = _frame + 1
    -- 根据游戏时间增加体力值
    --print("all time",os.clock()-_lastResTime, Tili_Restore, GetTili(),Tili_Max)
    if os.clock()-_lastResTime >= Tili_Restore then
        _lastResTime = os.clock()
        -- 恢复一点体力
        if GetTili() < Tili_Max then
            --print("add_1")
            AddTili(1)
        end
    end
    
    GameSound_Update()
    
    if _frame % 30 == 0 then
        Func_DataStatis(DS_GameTime, 1)
    end
    
    if #GameBuyItemsTables > 0 then
        local info = GameBuyItemsTables[1]
        table.remove(GameBuyItemsTables, 1)
        GameJavaBuyItems(info.id, info.tag, info.message)
    end
end
CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(update, 2.0/60, false)

local function hasWXHLunLock()
    if GetZJWGDatas(#Stage_Infos_ZJWG - 1) > 0 then
        return true
    end
    return false
end

local function hasZJWSunLock()
    --local hlwsIdx = 3
    --local _StageInfos = StageInfos_HLWS[hlwsIdx]
    --local score = GetStageDatas(hlwsIdx)
    ----print(score, _StageInfos[PScore])
    --if score >= _StageInfos[PScore] then
    --    return true
    --end
    
    ---判断是否获得解锁成就
    if _AchieveTable_Unlock[Ach_ModelZJWG] then
        return true
    end
    
    return false
end

-- create menu
local function initWithLayer()
    local _isSeting = false
    local mainMenu = nil
    
    local spLocksZJWG = nil
    
    -- 按钮回调函数
    local function menuCallback(tag)
        local iserror = false
        
        if tag == BtnHLWS then
            GameReplaceScne(Create_HLWS_Select_Scene())
        elseif tag == BtnSJSK then
            -- 判断体力进入
            if GetTili() < Tili_Souji then
                --剩余时间
                local nextT = Tili_Restore - (os.clock()-_lastResTime)
                CCDialogClose(string.format("您的体力不足,等待体力恢复后再继续吧\n(每4分钟恢复1点,当前0)\n下次恢复还需 %.0f 秒",nextT))
            else
                Create_SJSK_Scene()
            end
        elseif tag == BtnZJGW then
            if hasZJWSunLock() then
                Create_ZJWG_Select_Scene()
            else
                toJavaBuyItems(GAYX_Item_ZJWG)
                
                iserror = true
                Func_DataStatis(DS_ClickNil)
            end
        elseif tag == BtnWXHL then
            if hasWXHLunLock() then
                Create_WXHL_Scene()
            else
                iserror = true
                Func_DataStatis(DS_ClickNil)
            end
        elseif tag == BtnSD then
            GameReplaceScne(Create_Shop_Scene())
        elseif tag == BtnZY then
            GameReplaceScne(Create_Role_Scene())
        elseif tag == BtnCJ then
            GameReplaceScne(Create_Achieve_Scene())
        end
        
        local smefile = sme_Click
        if iserror then
            smefile = sme_ClickError
        end
        Play_Effect(smefile)
    end
    
    
    -- 背景
    local _bgSpr = nil
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bg)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight/2))
    layer:addChild(_bgSpr)
    _bgSpr = CCSprite:createWithSpriteFrameName(s_Space_bgHead)
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, 960-32))
    layer:addChild(_bgSpr, 9)
    
    _bgSpr = CCSprite:createWithSpriteFrameName("mainMemuBG.png")
    _bgSpr:setPosition(gdccp(DWinSizeWidth/2, DWinSizeHeight - 70))
    _bgSpr:setAnchorPoint(ccp(0.5, 1))
    layer:addChild(_bgSpr)
    
    
    
    -- 创建一个主菜单
    mainMenu = CCMenu:create()
    
    local invHeight = 110
    local bpx = DWinSizeWidth / 2
    local bpy = DWinSizeHeight - 180
    --魂力无双
    local btnItem = GFunc_CreateButtonP("mainBtnItem_01.png", menuCallback)
    btnItem:setPosition(gdccp(bpx, bpy))
    mainMenu:addChild(btnItem, LayerIdx_Bg, BtnHLWS)
    
    _bgSpr = CCSprite:createWithSpriteFrameName("ModeKuang.png")
    _bgSpr:setPosition(btnItem:getPosition())
    layer:addChild(_bgSpr, LayerIdx_Bg)
    
    --水晶时空
    local btnItem = GFunc_CreateButtonP("mainBtnItem_02.png", menuCallback)
    btnItem:setPosition(gdccp(bpx, bpy - invHeight * 1))
    mainMenu:addChild(btnItem, LayerIdx_Bg, BtnSJSK)
    
    _bgSpr = CCSprite:createWithSpriteFrameName("ModeKuang.png")
    _bgSpr:setPosition(btnItem:getPosition())
    layer:addChild(_bgSpr, LayerIdx_Bg)
    
    --拯救王国
    local btnItem = GFunc_CreateButtonP("mainBtnItem_03.png", menuCallback)
    btnItem:setPosition(gdccp(bpx, bpy - invHeight * 2))
    mainMenu:addChild(btnItem, LayerIdx_Bg, BtnZJGW)
    
    _bgSpr = CCSprite:createWithSpriteFrameName("ModeKuang.png")
    _bgSpr:setPosition(btnItem:getPosition())
    layer:addChild(_bgSpr, LayerIdx_Bg)
    
    
    --- 模式解锁
    --收集模式第四小关过关即可
    --if not hasZJWSunLock() then
    --    spLocksZJWG = CCSprite:createWithSpriteFrameName("ModeLock.png")
    --    spLocksZJWG:setPosition(gdccp(bpx + 125, bpy - invHeight * 2))
    --    layer:addChild(spLocksZJWG, LayerIdx_Gui)
    --end
    
    --无限回廊
    local btnItem = GFunc_CreateButtonP("mainBtnItem_04.png", menuCallback)
    btnItem:setPosition(gdccp(bpx, bpy - invHeight * 3))
    mainMenu:addChild(btnItem, LayerIdx_Bg, BtnWXHL)
    
    _bgSpr = CCSprite:createWithSpriteFrameName("ModeKuang.png")
    _bgSpr:setPosition(btnItem:getPosition())
    layer:addChild(_bgSpr, LayerIdx_Bg)
    
    
    
    --- 模式解锁
    --挑战模式最后一关通关
    if not hasWXHLunLock() then
        local spLocks = CCSprite:createWithSpriteFrameName("ModeLock.png")
        spLocks:setPosition(gdccp(bpx + 125, bpy - invHeight * 3))
        layer:addChild(spLocks, LayerIdx_Gui)
    end
    
    
    --职业
    bpx = 180
    bpy = 200
    _widthb = 78
    _heightb = 72
    local invWidth = 150
    local pos = gdccp(bpx, bpy)
    
    
    btnItemBg = CCSprite:createWithSpriteFrameName("mainMenuItemBG.png")
    btnItemBg:setPosition(gdccp(DWinSizeWidth/2, bpy))
    layer:addChild(btnItemBg, LayerIdx_Bg)
    
    -->背景
    local btnItem = GFunc_CreateButtonP("mainBtnUI_01.png", menuCallback)
    btnItem:setPosition(pos)
    mainMenu:addChild(btnItem, LayerIdx_Gui, BtnZY)
    --商店
    -->背景
    pos = gdccp(bpx + invWidth * 1, bpy)
    local btnItem = GFunc_CreateButtonP("mainBtnUI_02.png", menuCallback)
    btnItem:setPosition(pos)
    mainMenu:addChild(btnItem, LayerIdx_Gui, BtnSD)
    --成就
    -->背景
    pos = gdccp(bpx + invWidth * 2, bpy)
    local btnItem = GFunc_CreateButtonP("mainBtnUI_03.png", menuCallback)
    btnItem:setPosition(pos)
    mainMenu:addChild(btnItem, LayerIdx_Gui, BtnCJ)
    
    -- 主菜单大小
    mainMenu:setContentSize(CCSizeMake(DWinSizeWidth,  DWinSizeHeight))
    -- 设置坐标
    mainMenu:setPosition(0, 0)
    -- 添加至主菜单层
    layer:addChild(mainMenu, 1)
    
    
    
    ---- 模型查看测试
    --pos = gdccp(bpx + invWidth * 2+100, bpy)
    --btnSpriteN = CCSprite:createWithSpriteFrameName("mainBtnUI_03.png")
    --btnSpriteS = CCSprite:createWithSpriteFrameName("mainBtnUI_03.png")
    --btnSpriteS:setScale(1.1)
    --btnSpriteS:setContentSize(CCSizeMake(_widthb*1.1, _heightb*1.1))
    --btnItem = CCMenuItemSprite:create(btnSpriteN, btnSpriteS)
    --btnItem:registerScriptTapHandler(Create_RoleShow_Scene)
    --btnItem:setPosition(pos)
    --mainMenu:addChild(btnItem, LayerIdx_Gui)
    
    
    
    ---CCSpriterX测试
    --[[
    local sptest = CCSpriterX:create("Hero.SCML")
    print(sptest)
    sptest:setPosition(gdccp(380, 160))
    --sptest:setScale(0.6)
    sptest:PlayByName("Posture")
    layer:addChild(sptest)
    ]]--
    
    --local testbz = false
    -- 触摸回调响应
    local function onTouch(eventType, x, y)
        --local nlist = {}
        --for i=1,10 do
        --    table.insert(nlist, ccp(math.random(0,DWinSizeWidth), math.random(0,DWinSizeHeight)))
        --end
        --Lightning_New(nlist, nil)
        --if not testbz then
        --    GFunc_ShowBaozouEffs()
        --    testbz = true
        --else
        --    Gfunc_DelBazouEffs()
        --    testbz = false
        --end
        
        --GFunc_CreateShowAchieve(0x01)
        --
        --if eventType == 11 then
        --    CCDialogClose("返回按钮,,,,")
        --elseif eventType == 12 then
        --    CCDialogClose("菜单按钮....")
        --end
        --print(eventType, x, y)
        --GFunc_CreateShowMessage("通关条件", string.format("击杀 %d 名敌人。", 100), false)
        
        --toJavaHandler("com/android/lua/Cplus", "onBuyItem_AYX","(II)V", 130, 111)
        
        --GameJavaHandler(2,1,1)
    end
    
    
    -- 接受按键
    --layer:setKeypadEnabled(true)
    -- 接受触摸
    layer:setTouchEnabled(true)
    -- 触摸回调
    layer:registerScriptTouchHandler(onTouch)
    
    
    ----重置存档
    --local menu = CCMenu:create()
    --local btnItem = CCMenuItemLabel:create(CCLabelTTF:create("重置存档", "Arial", 28))
    --btnItem:registerScriptTapHandler(function()
    --    CCUserDefault:purgeSharedUserDefault()
    --    CCUserDefault:sharedUserDefault():flush()
    --end)
    --menu:addChild(btnItem)
    --menu:setPosition(gdccp(DWinSizeWidth-50, DWinSizeHeight - 50))
    --layer:addChild(menu,5)
end

function createLayerMenu()
    scene = CCScene:create()
    layer = CCLayer:create()
    initWithLayer()
    
    scene:addChild(layer)
    scene:addChild(CreateBackMenuItem(UI_Main))

    return scene, UI_Main
end




